package com.meisupic.copyright.util;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;
import java.util.regex.Pattern;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

public class ValidatorUtil {

	/**
	 * 正则表达式：验证手机号
	 */
	public static final String REGEX_MOBILE = "^1(3|4|5|7|8)\\d{9}$";

	/**
	 * 正则表达式：验证邮箱
	 */
	public static final String REGEX_EMAIL = "^([a-z0-9A-Z]+[_-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";

	/**
	 * 正则表达式：组织机构代码
	 */
	public static final String REGEX_ORGCODE = "^([0-9A-Z]){8}-[0-9|X]$";

	/**
	 * 正则表达式：营业执照号
	 */
	public static final String REGEX_BIZCODE = "^[1-6]\\d{14}$";

	/**
	 * 正则表达式：统一社会信用代码（三码合一）
	 */
	public static final String REGEX_UNICODE = "^([0-9A-Z]{2})([0-9]{6})([0-9A-Z]){10}$";

	/**
	 * 校验手机号
	 * 
	 * @param mobile
	 * @return 校验通过返回true，否则返回false
	 */
	public static boolean isMobile(String mobile) {
		return Pattern.matches(REGEX_MOBILE, mobile);
	}

	/**
	 * 校验邮箱
	 * 
	 * @param email
	 * @return 校验通过返回true，否则返回false
	 */
	public static boolean isEmail(String email) {
		return Pattern.matches(REGEX_EMAIL, email);
	}

	/**
	 * 校验身份证号码
	 * 
	 * @param IDCardNo
	 * @return 校验通过返回true，否则返回false
	 */
	public static boolean isIDCardNo(String IDCardNo) {
		IdCardNoValidator idCardNoValidator = new IdCardNoValidator();
		return idCardNoValidator.isValid(IDCardNo);
	}

	/**
	 * 检验组织机构代码
	 * 
	 * @param orgCode
	 * @return 校验通过返回true，否则返回false
	 */
	public static boolean isOrgCode(String orgCode) {
		boolean isValid = true;
		if (!Pattern.matches(REGEX_ORGCODE, orgCode)) {
			isValid = false;
		} else {
			int[] wi = { 3, 7, 9, 10, 5, 8, 4, 2 };
			String ci = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
			int sum = 0;
			for (int i = 0; i < 8; i++) {
				sum += ci.indexOf(String.valueOf(orgCode.charAt(i))) * wi[i];
			}
			int c9 = 11 - (sum % 11);
			String sc9 = String.valueOf(c9);
			if (11 == c9) {
				sc9 = "0";
			} else if (10 == c9) {
				sc9 = "X";
			}
			isValid = sc9.equals(String.valueOf(orgCode.charAt(9)));
		}
		return isValid;
	}

	/**
	 * 检验统一社会信用代码
	 * 
	 * @param unicode
	 * @return 校验通过返回true，否则返回false
	 */
	public static boolean isUnicode(String unicode) {

		if (!unicode.matches(REGEX_UNICODE)) {
			return false;
		}

		// 代码字符集-代码字符
		String[] charCode = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "T", "U", "W", "X", "Y" };
		String charcode = StringUtils.join(charCode);
		// 代码字符集-代码字符数值
		int[] charVal = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 };
		// 各位置序号上的加权因子
		int[] posWi = { 1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28 };

		// 校验位校验
		String[] obj = (String[]) ArrayUtils.removeElement(unicode.split(""), "");

		// ∑(ci×Wi)(mod 31)
		int sum = 0;
		int ci = 0;
		int Wi = 0;
		for (int i = 0; i < 17; i++) {

			ci = charVal[charcode.indexOf(obj[i])];
			Wi = posWi[i];
			sum += ci * Wi;
		}
		int c10 = 31 - (sum % 31);
		c10 = 31 == c10 ? 0 : c10;

		return c10 == charcode.indexOf(obj[17]);
	}

	/**
	 * 检验营业执照号
	 * 
	 * @param bizCode
	 * @return
	 */
	public static boolean isBizCode(String bizCode) {
		boolean isValid = true;
		if (!Pattern.matches(REGEX_BIZCODE, bizCode)) {
			isValid = false;
		} else {
			int[] s = new int[15];
			int[] p = new int[16];
			p[0] = 10;
			String[] obj = (String[]) ArrayUtils.removeElement(bizCode.split(""), "");
			for (int i = 0; i < 15; i++) {
				s[i] = (p[i] % 11) + Integer.valueOf(obj[i]);
				p[i + 1] = (s[i] % 10 == 0 ? 10 : s[i] % 10) * 2;
			}
			if (1 != s[14] % 10) {
				isValid = false;
			}
		}
		return isValid;
	}

	/**
	 * 判断参数是否非NULL,空字符串，空数组，空的Collection或Map(只有空格的字符串也认为是空串)
	 * 
	 * @param obj
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static boolean isEmpty(Object obj) {
		if (obj == null) {
			return true;
		}
		if (obj instanceof String && obj.toString().trim().length() == 0) {
			return true;
		}
		if (obj.getClass().isArray() && Array.getLength(obj) == 0) {
			return true;
		}
		if (obj instanceof Collection && ((Collection) obj).isEmpty()) {
			return true;
		}
		if (obj instanceof Map && ((Map) obj).isEmpty()) {
			return true;
		}
		return false;
	}

}
